JavaScriptMinifier: Fix "Uninitialized offset" in string and regexp parsing
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / JavaScriptMinifierTest.php
index d23534e..d6a1040 100644 (file)
 
 class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase {
 
 
 class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase {
 
+       use MediaWikiCoversValidator;
+
        public static function provideCases() {
        public static function provideCases() {
-               return array(
+               return [
 
                        // Basic whitespace and comments that should be stripped entirely
 
                        // Basic whitespace and comments that should be stripped entirely
-                       array( "\r\t\f \v\n\r", "" ),
-                       array( "/* Foo *\n*bar\n*/", "" ),
+                       [ "\r\t\f \v\n\r", "" ],
+                       [ "/* Foo *\n*bar\n*/", "" ],
 
                        /**
 
                        /**
-                        * Slashes used inside block comments (bug 26931).
+                        * Slashes used inside block comments (T28931).
                         * At some point there was a bug that caused this comment to be ended at '* /',
                         * causing /M... to be left as the beginning of a regex.
                         */
                         * At some point there was a bug that caused this comment to be ended at '* /',
                         * causing /M... to be left as the beginning of a regex.
                         */
-                       array(
+                       [
                                "/**\n * Foo\n * {\n * 'bar' : {\n * "
                                        . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
                                "/**\n * Foo\n * {\n * 'bar' : {\n * "
                                        . "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
-                               "" ),
+                               "" ],
 
                        /**
                         * '  Foo \' bar \
                         *  baz \' quox '  .
                         */
 
                        /**
                         * '  Foo \' bar \
                         *  baz \' quox '  .
                         */
-                       array(
+                       [
                                "'  Foo  \\'  bar  \\\n  baz  \\'  quox  '  .length",
                                "'  Foo  \\'  bar  \\\n  baz  \\'  quox  '.length"
                                "'  Foo  \\'  bar  \\\n  baz  \\'  quox  '  .length",
                                "'  Foo  \\'  bar  \\\n  baz  \\'  quox  '.length"
-                       ),
-                       array(
+                       ],
+                       [
                                "\"  Foo  \\\"  bar  \\\n  baz  \\\"  quox  \"  .length",
                                "\"  Foo  \\\"  bar  \\\n  baz  \\\"  quox  \".length"
                                "\"  Foo  \\\"  bar  \\\n  baz  \\\"  quox  \"  .length",
                                "\"  Foo  \\\"  bar  \\\n  baz  \\\"  quox  \".length"
-                       ),
-                       array( "// Foo b/ar baz", "" ),
-                       array(
+                       ],
+                       [ "// Foo b/ar baz", "" ],
+                       [
                                "/  Foo  \\/  bar  [  /  \\]  /  ]  baz  /  .length",
                                "/  Foo  \\/  bar  [  /  \\]  /  ]  baz  /.length"
                                "/  Foo  \\/  bar  [  /  \\]  /  ]  baz  /  .length",
                                "/  Foo  \\/  bar  [  /  \\]  /  ]  baz  /.length"
-                       ),
+                       ],
 
                        // HTML comments
 
                        // HTML comments
-                       array( "<!-- Foo bar", "" ),
-                       array( "<!-- Foo --> bar", "" ),
-                       array( "--> Foo", "" ),
-                       array( "x --> y", "x-->y" ),
+                       [ "<!-- Foo bar", "" ],
+                       [ "<!-- Foo --> bar", "" ],
+                       [ "--> Foo", "" ],
+                       [ "x --> y", "x-->y" ],
 
                        // Semicolon insertion
 
                        // Semicolon insertion
-                       array( "(function(){return\nx;})", "(function(){return\nx;})" ),
-                       array( "throw\nx;", "throw\nx;" ),
-                       array( "while(p){continue\nx;}", "while(p){continue\nx;}" ),
-                       array( "while(p){break\nx;}", "while(p){break\nx;}" ),
-                       array( "var\nx;", "var x;" ),
-                       array( "x\ny;", "x\ny;" ),
-                       array( "x\n++y;", "x\n++y;" ),
-                       array( "x\n!y;", "x\n!y;" ),
-                       array( "x\n{y}", "x\n{y}" ),
-                       array( "x\n+y;", "x+y;" ),
-                       array( "x\n(y);", "x(y);" ),
-                       array( "5.\nx;", "5.\nx;" ),
-                       array( "0xFF.\nx;", "0xFF.x;" ),
-                       array( "5.3.\nx;", "5.3.x;" ),
+                       [ "(function(){return\nx;})", "(function(){return\nx;})" ],
+                       [ "throw\nx;", "throw\nx;" ],
+                       [ "while(p){continue\nx;}", "while(p){continue\nx;}" ],
+                       [ "while(p){break\nx;}", "while(p){break\nx;}" ],
+                       [ "var\nx;", "var x;" ],
+                       [ "x\ny;", "x\ny;" ],
+                       [ "x\n++y;", "x\n++y;" ],
+                       [ "x\n!y;", "x\n!y;" ],
+                       [ "x\n{y}", "x\n{y}" ],
+                       [ "x\n+y;", "x+y;" ],
+                       [ "x\n(y);", "x(y);" ],
+                       [ "5.\nx;", "5.\nx;" ],
+                       [ "0xFF.\nx;", "0xFF.x;" ],
+                       [ "5.3.\nx;", "5.3.x;" ],
+
+                       // Cover failure case for incomplete hex literal
+                       [ "0x;", false, false ],
+
+                       // Cover failure case for number with no digits after E
+                       [ "1.4E", false, false ],
+
+                       // Cover failure case for number with several E
+                       [ "1.4EE2", false, false ],
+                       [ "1.4EE", false, false ],
+
+                       // Cover failure case for number with several E (nonconsecutive)
+                       // FIXME: This is invalid, but currently tolerated
+                       [ "1.4E2E3", "1.4E2 E3", false ],
 
                        // Semicolon insertion between an expression having an inline
 
                        // Semicolon insertion between an expression having an inline
-                       // comment after it, and a statement on the next line (bug 27046).
-                       array(
+                       // comment after it, and a statement on the next line (T29046).
+                       [
                                "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
                                "var a=this\nfor(b=0;c<d;b++){}"
                                "var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
                                "var a=this\nfor(b=0;c<d;b++){}"
-                       ),
+                       ],
+
+                       // Cover failure case of incomplete regexp at end of file (T75556)
+                       // FIXME: This is invalid, but currently tolerated
+                       [ "*/", "*/", false ],
+
+                       // Cover failure case of incomplete string at end of file (T75556)
+                       // FIXME: This is invalid, but currently tolerated
+                       [ "'a", "'a", false ],
 
                        // Token separation
 
                        // Token separation
-                       array( "x  in  y", "x in y" ),
-                       array( "/x/g  in  y", "/x/g in y" ),
-                       array( "x  in  30", "x in 30" ),
-                       array( "x  +  ++  y", "x+ ++y" ),
-                       array( "x ++  +  y", "x++ +y" ),
-                       array( "x  /  /y/.exec(z)", "x/ /y/.exec(z)" ),
+                       [ "x  in  y", "x in y" ],
+                       [ "/x/g  in  y", "/x/g in y" ],
+                       [ "x  in  30", "x in 30" ],
+                       [ "x  +  ++  y", "x+ ++y" ],
+                       [ "x ++  +  y", "x++ +y" ],
+                       [ "x  /  /y/.exec(z)", "x/ /y/.exec(z)" ],
 
                        // State machine
 
                        // State machine
-                       array( "/  x/g", "/  x/g" ),
-                       array( "(function(){return/  x/g})", "(function(){return/  x/g})" ),
-                       array( "+/  x/g", "+/  x/g" ),
-                       array( "++/  x/g", "++/  x/g" ),
-                       array( "x/  x/g", "x/x/g" ),
-                       array( "(/  x/g)", "(/  x/g)" ),
-                       array( "if(/  x/g);", "if(/  x/g);" ),
-                       array( "(x/  x/g)", "(x/x/g)" ),
-                       array( "([/  x/g])", "([/  x/g])" ),
-                       array( "+x/  x/g", "+x/x/g" ),
-                       array( "{}/  x/g", "{}/  x/g" ),
-                       array( "+{}/  x/g", "+{}/x/g" ),
-                       array( "(x)/  x/g", "(x)/x/g" ),
-                       array( "if(x)/  x/g", "if(x)/  x/g" ),
-                       array( "for(x;x;{}/  x/g);", "for(x;x;{}/x/g);" ),
-                       array( "x;x;{}/  x/g", "x;x;{}/  x/g" ),
-                       array( "x:{}/  x/g", "x:{}/  x/g" ),
-                       array( "switch(x){case y?z:{}/  x/g:{}/  x/g;}", "switch(x){case y?z:{}/x/g:{}/  x/g;}" ),
-                       array( "function x(){}/  x/g", "function x(){}/  x/g" ),
-                       array( "+function x(){}/  x/g", "+function x(){}/x/g" ),
+                       [ "/  x/g", "/  x/g" ],
+                       [ "(function(){return/  x/g})", "(function(){return/  x/g})" ],
+                       [ "+/  x/g", "+/  x/g" ],
+                       [ "++/  x/g", "++/  x/g" ],
+                       [ "x/  x/g", "x/x/g" ],
+                       [ "(/  x/g)", "(/  x/g)" ],
+                       [ "if(/  x/g);", "if(/  x/g);" ],
+                       [ "(x/  x/g)", "(x/x/g)" ],
+                       [ "([/  x/g])", "([/  x/g])" ],
+                       [ "+x/  x/g", "+x/x/g" ],
+                       [ "{}/  x/g", "{}/  x/g" ],
+                       [ "+{}/  x/g", "+{}/x/g" ],
+                       [ "(x)/  x/g", "(x)/x/g" ],
+                       [ "if(x)/  x/g", "if(x)/  x/g" ],
+                       [ "for(x;x;{}/  x/g);", "for(x;x;{}/x/g);" ],
+                       [ "x;x;{}/  x/g", "x;x;{}/  x/g" ],
+                       [ "x:{}/  x/g", "x:{}/  x/g" ],
+                       [ "switch(x){case y?z:{}/  x/g:{}/  x/g;}", "switch(x){case y?z:{}/x/g:{}/  x/g;}" ],
+                       [ "function x(){}/  x/g", "function x(){}/  x/g" ],
+                       [ "+function x(){}/  x/g", "+function x(){}/x/g" ],
 
                        // Multiline quoted string
 
                        // Multiline quoted string
-                       array( "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ),
+                       [ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
 
                        // Multiline quoted string followed by string with spaces
 
                        // Multiline quoted string followed by string with spaces
-                       array(
+                       [
                                "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
                                "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
                                "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
                                "var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
-                       ),
+                       ],
 
                        // URL in quoted string ( // is not a comment)
 
                        // URL in quoted string ( // is not a comment)
-                       array(
+                       [
                                "aNode.setAttribute('href','http://foo.bar.org/baz');",
                                "aNode.setAttribute('href','http://foo.bar.org/baz');"
                                "aNode.setAttribute('href','http://foo.bar.org/baz');",
                                "aNode.setAttribute('href','http://foo.bar.org/baz');"
-                       ),
+                       ],
 
                        // URL in quoted string after multiline quoted string
 
                        // URL in quoted string after multiline quoted string
-                       array(
+                       [
                                "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
                                "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
                                "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
                                "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
-                       ),
+                       ],
 
                        // Division vs. regex nastiness
 
                        // Division vs. regex nastiness
-                       array(
+                       [
                                "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
                                "alert((10+10)/'/'.charCodeAt(0)+'//');"
                                "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
                                "alert((10+10)/'/'.charCodeAt(0)+'//');"
-                       ),
-                       array( "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ),
+                       ],
+                       [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
 
                        // newline insertion after 1000 chars: break after the "++", not before
 
                        // newline insertion after 1000 chars: break after the "++", not before
-                       array( str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ),
+                       [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
 
 
-                       // Unicode letter characters should pass through ok in identifiers (bug 31187)
-                       array( "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ),
+                       // Unicode letter characters should pass through ok in identifiers (T33187)
+                       [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
 
                        // Per spec unicode char escape values should work in identifiers,
                        // as long as it's a valid char. In future it might get normalized.
 
                        // Per spec unicode char escape values should work in identifiers,
                        // as long as it's a valid char. In future it might get normalized.
-                       array( "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ),
+                       [ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
 
                        // Some structures that might look invalid at first sight
 
                        // Some structures that might look invalid at first sight
-                       array( "var a = 5.;", "var a=5.;" ),
-                       array( "5.0.toString();", "5.0.toString();" ),
-                       array( "5..toString();", "5..toString();" ),
-                       array( "5...toString();", false ),
-                       array( "5.\n.toString();", '5..toString();' ),
+                       [ "var a = 5.;", "var a=5.;" ],
+                       [ "5.0.toString();", "5.0.toString();" ],
+                       [ "5..toString();", "5..toString();" ],
+                       // Cover failure case for too many decimal points
+                       [ "5...toString();", false ],
+                       [ "5.\n.toString();", '5..toString();' ],
 
                        // Boolean minification (!0 / !1)
 
                        // Boolean minification (!0 / !1)
-                       array( "var a = { b: true };", "var a={b:!0};" ),
-                       array( "var a = { true: 12 };", "var a={true:12};", false ),
-                       array( "a.true = 12;", "a.true=12;", false ),
-                       array( "a.foo = true;", "a.foo=!0;" ),
-                       array( "a.foo = false;", "a.foo=!1;" ),
-               );
+                       [ "var a = { b: true };", "var a={b:!0};" ],
+                       [ "var a = { true: 12 };", "var a={true:12};", false ],
+                       [ "a.true = 12;", "a.true=12;", false ],
+                       [ "a.foo = true;", "a.foo=!0;" ],
+                       [ "a.foo = false;", "a.foo=!1;" ],
+               ];
        }
 
        /**
         * @dataProvider provideCases
         * @covers JavaScriptMinifier::minify
        }
 
        /**
         * @dataProvider provideCases
         * @covers JavaScriptMinifier::minify
+        * @covers JavaScriptMinifier::parseError
         */
         */
-       public function testJavaScriptMinifierOutput( $code, $expectedOutput, $expectedValid = true ) {
+       public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
                $minified = JavaScriptMinifier::minify( $code );
 
                // JSMin+'s parser will throw an exception if output is not valid JS.
                $minified = JavaScriptMinifier::minify( $code );
 
                // JSMin+'s parser will throw an exception if output is not valid JS.
@@ -174,21 +200,21 @@ class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase {
        }
 
        public static function provideExponentLineBreaking() {
        }
 
        public static function provideExponentLineBreaking() {
-               return array(
-                       array(
+               return [
+                       [
                                // This one gets interpreted all together by the prior code;
                                // no break at the 'E' happens.
                                '1.23456789E55',
                                // This one gets interpreted all together by the prior code;
                                // no break at the 'E' happens.
                                '1.23456789E55',
-                       ),
-                       array(
+                       ],
+                       [
                                // This one breaks under the bad code; splits between 'E' and '+'
                                '1.23456789E+5',
                                // This one breaks under the bad code; splits between 'E' and '+'
                                '1.23456789E+5',
-                       ),
-                       array(
+                       ],
+                       [
                                // This one breaks under the bad code; splits between 'E' and '-'
                                '1.23456789E-5',
                                // This one breaks under the bad code; splits between 'E' and '-'
                                '1.23456789E-5',
-                       ),
-               );
+                       ],
+               ];
        }
 
        /**
        }
 
        /**